<?php
class MessageController extends AppController
{
	
	var $autoRender = true;
    var $uses = array( 'Fans','Like_info','Contacter','Letter','Users', 'Reply_floor','Reply_layer','Reply_me','Topic','Groups' );
    var $layout = 'default';
    private $helpers;
    private $perpage;
    private $page;
    private $components;
    private $com_ctl;
    function init()
    {
    	$this->com_ctl = new Com_ctl();
    	$this->helpers = array( 'html' );
    	$this->page = 1;
    	$this->components = array( 'pager' );
    	$this->perpage = 50;
    }
    function my_attend() //我关注的人
    {
    	$user_id = $_SESSION['user_id'];
    	$where = array(	'fans_id' => $user_id );
    	$count = $this->Subject_item->findCount( $where );
    	$ret = $this->Fans->findAll( $where, null, null, $this->perpage, $this->page );
    	foreach ( $ret as $val )
    	{
    		$val = $val['Fans'];
    	}
    	$pager_cfg = array(
                'prevImg' => '上一页',
                'nextImg' => '下一页',
                'totalItems' => $count,
                'perPage' => $this->perpage,
                'currentPage' => $this->page
        );
        $this->pager->init($pager_cfg);
    }
    function my_fans()//我的粉丝
    {
    	$user_id = $_SESSION['user_id'];
    	$where = array(	'user_id' => $user_id );
    	$count = $this->Subject_item->findCount( $where );
    	$ret = $this->Fans->findAll( $where, null, null, $this->perpage, $this->page );
    	foreach ( $ret as $val )
    	{
    		$val = $val['Fans'];
    	}
    	$pager_cfg = array(
                'prevImg' => '上一页',
                'nextImg' => '下一页',
                'totalItems' => $count,
                'perPage' => $this->perpage,
                'currentPage' => $this->page
        );
        $this->pager->init($pager_cfg);
    }
    function like_me()//喜欢我的
    {
    	$where = array( 'pub_user_id' => $_SESSION['user_id'] );
    	$count = $this->Subject_item->findCount( $where );
    	$ret = $this->Like_info->findAll( $where, null, null, $this->perpage, $this->page );
    	foreach ( $ret as $val )
    	{
    		$val = $val['Like_info'];
    	}
    	$pager_cfg = array(
                'prevImg' => '上一页',
                'nextImg' => '下一页',
                'totalItems' => $count,
                'perPage' => $this->perpage,
                'currentPage' => $this->page
        );
        $this->pager->init($pager_cfg);
    }
	function get_contacter( $page = 1 )//私信
    {
    	$this->page = $page;
    	$user_id = $_SESSION['user_id'];
    	$letters = array();
    	$contacters = array();
    	$count = $this->Contacter->findCount( "from_user_id=$user_id or to_user_id=$user_id" );
		$ret_con = $this->Contacter->findAll( "from_user_id=$user_id or to_user_id=$user_id", null, 'create_time desc', $this->perpage, $this->page );
    	foreach( $ret_con as $key => $val )
    	{
    		$val = $val['Contacter'];
    		$where = array( 'contact_id' => $val['contact_id'] );
    		$ret_letter = $this->Letter->findAll( $where, null, 'create_time desc', 1 );
    		$unread_letter = $this->get_unread_letter( get_unread_letter );
    		$content = isset( $ret_letter[0]['Letter']['content'] )?$ret_letter[0]['Letter']['content']:'';
    		$sub_content = get_sub_str( $content );
    		if( $val['from_user_id'] == $user_id )
    		{
    			$another_id = $val['to_user_id'];
    		}
    		else 
    		{
    			$another_id = $val['from_user_id'];
    		}
    		$ret_user = $this->Users->findAll( array( 'id' => $another_id ), null, null, 1 );
    		$another_into = $ret_user[0]['Users'];
            $contacters[$key] = array( 'user_name' => $ret_user[0]['nick_name'],'content' => $sub_content, 'update_time' => $val['update_time'] );
    		unset( $ret_user );
    		
	    	if( $this->page == 1 && $key == 0 ) //如果是第一页，这展示和最新联系人的聊天记录
		    {
				$letters = $this->get_letter( $val['contact_id'] );
		    }
    	}
    }
	function get_letter( $contact_id, $use_view=true )//私信
    {
  		$where = array( 'contact_id' => $contact_id );
  		$this->Letter->set_db( substr( $contact_id, 0, 4 ) );
		$ret = $this->Letter->findAll( $where );
		$count = 0;
		$letters = array();
		$another_into = array();
    	foreach( $ret as $val )
    	{
    		$val = $val['Letter'];
    		if( $count == 0 )
    		{
    			if( $val['from_user_id'] == $_SESSION['user_id'] )
    			{
    				$another_id = $val['to_user_id'];
    			}
    			else 
    			{
    				$another_id = $val['from_user_id'];
    			}
    			$ret_user = $this->Users->findAll( array( 'id' => $another_id ), null, null, 1 );
    			$another_into = $ret_user[0]['Users'];
    			unset( $ret_user );
    		}
    		if( $val['from_user_id'] == $_SESSION['user_id'] )
    		{
    			$user_name = '我';
   			}
   			else 
   			{
   				$user_name = $another_into['nick_name'];
   			}
    		$letters[$count] = array( 'user_name' => $user_name,'content' => $val['content'], 'create_time' => $val['create_time'] );
    		$count ++;
    	}
    	if( $use_view === true )//如果使用模板
    	{
    		
    	}
    	else 
    	{
    		return $letters;
    	}
    }
    function get_unread_letter( $connact_id='' )//返回未读的私信数,有联系id则查当前联系id下单未读私信，否则查找当前操作人呢名下的所有未读私信
    {
    	if( $connact_id == '' )
    	{
    		$where = array( 'to_user_id' => $_SESSION['user_id'], 'status' => 0 );
    	}
    	else 
    	{
    		$where = array( 'contact_id' => $connact_id, 'status' => 0 );
    	}
    	$count = $this->Letter->findCount( $where );
    	return $count;
    }
    function set_letter( $to_user_id, $content )//发送私信
    {
    	$content = filter_content( $content );
    	$curr_time = date( 'Y-m-d H:i:s' );
    	$from_user_id = $_SESSION['user_id'];
    	$ret_con = $this->Contacter->findAll( "from_user_id=$from_user_id and to_user_id=$to_user_id or from_user_id=$to_user_id and to_user_id=$from_user_id", 
    				null, null, 1 );
    	$contact_data = array();
    	$letter_data = array();
    	if( isset( $ret_con[0]['Contacter']['contact_id'] ) )
    	{
    		$contact_id = $ret_con[0]['Contacter']['contact_id'];
    		$contact_data['id'] = $ret_con[0]['Contacter']['id'];
    		$contact_data['update_time'] = date( 'Y-m-d H:i:s' );
    	}
    	else 
    	{
    		$contact_id = get_contact_id();
    		$contact_data['id'] = null;
    		$contact_data['contact_id'] = $contact_id;
    		$contact_data['from_user_id'] = $from_user_id;
    		$contact_data['to_user_id'] = $to_user_id;
    		$contact_data['update_time'] = $curr_time;
    		$contact_data['create_time'] = $curr_time;
    	}
    	$this->Contacter->save( $contact_data );
    	$letter_data['id'] = null;
    	$letter_data['contact_id'] = $contact_id;
    	$letter_data['from_user_id'] = $from_user_id;
    	$letter_data['to_user_id'] = $to_user_id;
    	$letter_data['content'] = $content;
    	$letter_data['create_time'] = $curr_time;
		$this->Letter->save( $letter_data );
    }
	function reply_me( $page )//回复我的
    {
    	$topic_ids = array();
    	$layer_ids = array();
    	$group_ids = array();
    	$from_users = array();
    	$result = array();
    	$this->page = $page;
    	$where = array( 'user_id' => $_SESSION['user_id'], 'type' => 0 );
    	$orders = array( 'status' => 'asc', 'create_time' => 'desc' );
    	$ret = $this->Reply_me->findAll( $where, null, $orders, $this->perpage, $this->page );
    	foreach( $ret as $val )
    	{
    		$val = $val['Reply_me'];
    		$result[] = array( 
    			'topic_id' => $val['topic_id'],
    			'layer_id' => $val['layer_id'],
    			'content' => ''
    		);
    		$topic_ids[] = $val['topic_id'];
    		$layer_ids[] = $val['layer_id'];
    	}
    	//查主题topic信息。
    	$topic_ids = implode( "','", $topic_ids );
    	$result_topic = array();
    	$ret_topic = $this->Topic->findAll( "topic_id in( '$topic_ids' )", null, null, $this->perpage );
    	foreach( $ret_topic as $val )
    	{
    		$val = $val['Topic'];
    		$result_topic[$val['topic_id']] = get_sub_str( $val['title'] );
    		$group_ids[$val['topic_id']] = $val['group_id'];
    	}
    	//查层layer信息
    	$layer_ids = implode( "','", $layer_ids );
    	$result_layer = array();
    	$ret_layer = $this->Reply_layer->findAll( "layer_id in( '$layer_ids' )", null, null, $this->perpage );
    	foreach( $ret_layer as $val )
    	{
    		$val = $val['Reply_layer'];
    		$result_layer[$val['layer_id']] = get_sub_str( $val['content'] );
    		$from_users[$val['layer_id']] = $val['from_user_id'];
    	}
    	//查组信息。
    	$group_ids_str = implode( ",", $group_ids );
    	$result_group = array();
    	$ret_group = $this->Groups->findAll( "group_id in( $group_ids_str )", null, null, $this->perpage );
    	foreach( $ret_group as $val )
    	{
    		$val = $val['Groups'];
    		$result_group[$val['id']] = get_sub_str( $val['name'] );
    	}
    	//查用户信息。
    	$from_users_str = implode( ",", $from_users );
    	$result_user = array();
    	$ret_user = $this->Users->findAll( "id in( $from_users_str )", null, null, $this->perpage );
    	foreach( $ret_user as $val )
    	{
    		$val = $val['Users'];
    		$result_user[$val['id']] = $val['nick_name'];
    	}
    	
    	foreach( $result as $key => $val )
    	{
    		$result[$key]['topic_title'] = $result_topic[$val['topic_id']];
    		$result[$key]['content'] = $result_layer[$val['layer_id']];
    		$result[$key]['group_name'] = $result_group[$group_ids[$val['topic_id']]];
    		$result[$key]['from_user'] = $result_user[$from_users[$val['layer_id']]];
    	}
    }
    function mention_me()//提到我的
    {
    	$topic_ids = array();
    	$layer_ids = array();
    	$group_ids = array();
    	$from_users = array();
    	$floor_ids = array();
    	$result = array();
    	$this->page = $page;
    	$where = array( 'user_id' => $_SESSION['user_id'], 'type' => 1 );
    	$orders = array( 'status' => 'asc', 'create_time' => 'desc' );
    	$ret = $this->Reply_me->findAll( $where, null, $orders, $this->perpage, $this->page );
    	foreach( $ret as $val )
    	{
    		$val = $val['Reply_me'];
    		$result[] = array( 
    			'topic_id' => $val['topic_id'],
    			'layer_id' => $val['layer_id'],
    			'floor_id' => $val['floor_id'],
    			'content' => ''
    		);
    		$topic_ids[] = $val['topic_id'];
    		if( strlen( $val['layer_id'] ) > 0 )
    		{
    			$floor_ids[] = $val['floor_id'];
    		}
    		$layer_ids[] = $val['layer_id'];
    		
    	}
    	//查主题topic信息。
    	$topic_ids = implode( "','", $topic_ids );
    	$result_topic = array();
    	$ret_topic = $this->Topic->findAll( "topic_id in( '$topic_ids' )", null, null, $this->perpage );
    	foreach( $ret_topic as $val )
    	{
    		$val = $val['Topic'];
    		$result_topic[$val['topic_id']] = get_sub_str( $val['title'] );
    		$group_ids[$val['topic_id']] = $val['group_id'];
    	}
    	
    	if( count( $layer_ids ) > 0 )
    	{
	    	//查层layer信息
	    	$layer_ids = implode( "','", $layer_ids );
	    	$result_layer = array();
	    	$ret_layer = $this->Reply_layer->findAll( "layer_id in( '$layer_ids' )", null, null, $this->perpage );
	    	foreach( $ret_layer as $val )
	    	{
	    		$val = $val['Reply_layer'];
	    		$result_layer[$val['layer_id']] = get_sub_str( $val['content'] );
	    		$from_users['l'.$val['layer_id']] = $val['from_user_id'];
	    	}
    	}
   		if( count( $floor_ids ) > 0 )
    	{
	    	//查楼floor信息
	    	$floor_ids = implode( "','", $floor_ids );
	    	$result_floor = array();
	    	$ret_floor = $this->Reply_floor->findAll( "floor_id in( '$floor_ids' )", null, null, $this->perpage );
	    	foreach( $ret_floor as $val )
	    	{
	    		$val = $val['Reply_floor'];
	    		$result_floor[$val['floor_id']] = get_sub_str( $val['content'] );
	    		$from_users['f'.$val['floor_id']] = $val['user_id'];
	    	}
    	}
    	//查组信息。
    	$group_ids_str = implode( ",", $group_ids );
    	$result_group = array();
    	$ret_group = $this->Groups->findAll( "group_id in( $group_ids_str )", null, null, $this->perpage );
    	foreach( $ret_group as $val )
    	{
    		$val = $val['Groups'];
    		$result_group[$val['id']] = get_sub_str( $val['name'] );
    	}
    	//查用户信息。
    	$from_users_str = implode( ",", $from_users );
    	$result_user = array();
    	$ret_user = $this->Users->findAll( "id in( $from_users_str )", null, null, $this->perpage );
    	foreach( $ret_user as $val )
    	{
    		$val = $val['Users'];
    		$result_user[$val['id']] = $val['nick_name'];
    	}
    	
    	foreach( $result as $key => $val )
    	{
    		$result[$key]['topic_title'] = $result_topic[$val['topic_id']];
    		$result[$key]['group_name'] = $result_group[$group_ids[$val['topic_id']]];
    		if( strlen( $val['layer_id'] ) > 0 )
    		{
    			$result[$key]['content'] = $result_layer[$val['layer_id']];
    			$result[$key]['from_user'] = $result_user[$from_users['l'.$val['layer_id']]];
    		}
    		else 
    		{
    			$result[$key]['content'] = $result_floor[$val['floor_id']];
    			$result[$key]['from_user'] = $result_user[$from_users['f'.$val['floor_id']]];
    		}
    	}
    }
    
    function set_reply_floor( $topic_id, $content ) //回复主题，楼回复。
    {
    	$curr_time = date( 'Y-m-d H:i:s' );
    	$floor_id = get_common_id();
		$insert_data = array(
			'id' => null,
			'topic_id' => $topic_id,
			'floor_id' => $floor_id,
			'content' => filter_content( $content ),
			'user_id' => $_SESSION['user_id'],
			'create_time' => $curr_time,
			'update_time' => $curr_time
		);
		$this->Reply_floor->set_db( substr( $topic_id, 0, 4 ) );
    	$this->Reply_floor->save( $insert_data );
    	$mention_users = $this->g_get_mention_user( $content );
    	if( count( $mention_users ) > 0 )
    	{
    		foreach( $mention_users as $val )
    		{
    			$mention_data = array(
		    		'id' => null,
		    		'type' => 1,
		    		'topic_id' => $topic_id,
		    		'floor_id' => $floor_id,
		    		'layer_id' => '',
		    		'user_id' => $val,
		    		'status' => 0,
		    		'create_time' => $curr_time
		    	);
		    	$this->Reply_me->save( $mention_data );
		   	}
    	}
    	return true;
    }
    function set_reply_layer( $to_user_id, $topic_id, $floor_id, $content )//回复某人，层回复。
    {
    	$curr_time = date( 'Y-m-d H:i:s' );
    	$layer_id = get_common_id();
		$insert_data = array(
			'id' => null,
			'layer_id' => $layer_id,
			'floor_id' => $floor_id,
			'content' => filter_content( $content ),
			'from_user_id' => $_SESSION['user_id'],
			'to_user_id' => $to_user_id,
			'create_time' => $curr_time,
			'update_time' => $curr_time
		);
		$this->Reply_layer->set_db( substr( $topic_id, 0, 4 ) );
    	$this->Reply_layer->save( $insert_data );
    	$to_me_data = array(
    		'id' => null,
    		'type' => 0,
    		'topic_id' => $topic_id,
    		'floor_id' => $floor_id,
    		'layer_id' => $layer_id,
    		'user_id' => $user_id,
    		'status' => 0,
    		'create_time' => $curr_time
    	);
    	$this->Reply_me->save( $to_me_data );
    	$mention_users = $this->g_get_mention_user( $content );
    	if( count( $mention_users ) > 0 )
    	{
    		foreach( $mention_users as $val )
    		{
    			$mention_data = array(
		    		'id' => null,
		    		'type' => 1,
		    		'topic_id' => $topic_id,
		    		'floor_id' => $floor_id,
		    		'layer_id' => $layer_id,
		    		'user_id' => $val,
		    		'status' => 0,
		    		'create_time' => $curr_time
		    	);
		    	$this->Reply_me->save( $mention_data );
		   	}
    	}
    	return true;
    }  
    function set_like( $goods_id )//点击喜欢按钮，设置喜欢信息。仅审核通过的商品可以点击喜欢
    {
    	if( strlen( $goods_id )> 0 )
    	{
    		$where = array( 'goods_id' => $goods_id );
    		$ret = $this->Goods->findAll( $where, null, null, 1 );//取出一条即返回。
    		if( isset( $ret[0]['Goods']['goods_id'] ))
    		{
    			$curr_time = date( 'Y-m-d H:i:s' );
    			$insert_data = array(
    				'id' => null,
    				'goods_id' => $ret[0]['Goods']['goods_id'],
    				'pub_user_id' => $ret[0]['Goods']['user_id'],
    				'like_user_id' => $_SESSION['user_id'],
    				'create_time' => $curr_time,
    				'update_time' => $curr_time
    			);
    			$this->Like_info->save( $insert_data );
    			$this->com_ctl->set_like_count( $ret[0]['Goods']['user_id'] );
    			return true;
    		}
    	}
    	return false;
    }
    function g_get_mention_user( $content )
    {
    	$user_ids = array();
    	$content = str_repeat( '  ', ' ', $content );
    	$content = explode( ' ', $content );
    	foreach ( $content as $key => $val )
    	{
    		if( strpos( $val, '@' ) !== false )
    		{
    			$nick_name = str_replace( '@', '', $val );
    			$ret = $this->Users->findAll( array( 'nick_name' => $nick_name ), null, null, 1 );
    			if( count( $ret ) > 0 )
    			{
    				$user_ids[] = $ret[0]['Users']['id'];
    			}
    			unset( $ret );
    		}
    	}
    	return $user_ids;
    }
}